//package com.caiyi.financial.nirvana.sms.scheduled;
//
//import com.alibaba.fastjson.JSON;
//import com.caiyi.financial.nirvana.sms.enums.SmsUserTypeEnum;
//import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
//import com.caiyi.financial.nirvana.sms.jedis.JedisService;
//import com.caiyi.financial.nirvana.sms.queue.AlarmSendQueueEntity;
//import com.caiyi.financial.nirvana.sms.service.SmsMsgService;
//import com.caiyi.financial.nirvana.sms.util.Constant;
//import com.caiyi.financial.nirvana.sms.vo.SmsRateVo;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.util.List;
//
//@Component
//public class SuccessRateTask {
//
//    private static final Logger logger = LoggerFactory.getLogger(SuccessRateTask.class);
//    private static final String GATEWAY_ID = "1";
//    @Value("${spring.profiles.active}")
//    private String env;
//    @Autowired
//    private SmsMsgService smsMsgService;
//
//    /**
//     * Last 60分钟 - 120分钟，成功率监控
//     * 每60分钟执行一次
//     */
//    @Scheduled(fixedRate = Constant.TEN_MINUTES_OF_SECOND * 6 * 1000, initialDelay = Constant.TEN_MINUTES_OF_SECOND * 6 * 1000)
//    public void successRate() {
//        try {
//            List<SmsRateVo> list = smsMsgService.getMsgSuccessRate();
//            //循环所有通道成功率，插入redis报警队列中
//            for (SmsRateVo smsRateVo : list) {
//                String gatewayName = smsRateVo.getName();
//                String gatewayId = smsRateVo.getGatewayId();
//                Integer gatewayType = smsRateVo.getGatewayType();
//                Double msgCount = smsRateVo.getTotalCount();
//                Double successCount = 0d;
//                if (smsRateVo.getSuccessCount() != null) {
//                    successCount = smsRateVo.getSuccessCount();
//                }
//                Double successRate = Math.floor(successCount / msgCount * 100);
//                logger.info("SuccessRateTask 成功率监控 gatewayName:{}, successCount : {}, msgCount : {}, successRate : {}", gatewayName, successCount, msgCount, successRate);
//                if (msgCount > 0 && successRate < Constant.SUCCESS_RATE) {
//                    AlarmSendQueueEntity entity = new AlarmSendQueueEntity();
//                    entity.setPhone(Constant.PHONE);
//                    String content = String.format(Constant.SMS_SUCCESS_RATE_CONTENT, gatewayName, successRate + "%", Constant.SUCCESS_RATE + "%");
//                    entity.setContent(content);
//                    if (msgCount < 20 || SmsUserTypeEnum.MARKETING.index == gatewayType) {
//                        continue;
//                    }
//                    if (env.equals(Constant.ENV_PRO) && GATEWAY_ID.equals(gatewayId)) {
//                        logger.info("SuccessRateTask 测试通道成功率监控日志 : {}, content : {}", Constant.PHONE, content);
//                    } else {
//                        logger.info("SuccessRateTask 成功率监控报警 : {}, content : {}", Constant.PHONE, content);
//                        JedisService.lpush(JedisKey.SMS_QUEUE_ALARM_MSG_SEND, JSON.toJSONString(entity));
//                    }
//                } else {
//                    logger.info("SuccessRateTask 成功率监控运行完成,数据正常. 通道名称：{}，总数：{}，成功数：{}，成功率：{}", gatewayName, msgCount, successCount, successRate);
//                }
//            }
//
//        } catch (Exception e) {
//            logger.error("SuccessRateTask 成功率监控 run error", e);
//        }
//    }
//
//}
